/* 
 * 2018-11 WQ添加
 * admin模块负责管理框架的一些事件、效果，比如侧边栏的折叠效果等，以及封装了一些常用的公共方法
 */
layui.define(["layer", "config"], function(exports) {
    var b = layui.jquery,
        f = layui.layer,
        config = layui.config,
        e = {
            // 设置侧栏折叠
            flexible: function(a) {
                b(".layui-layout-admin").hasClass("admin-nav-mini") != !a && (a ? b(".layui-layout-admin").removeClass("admin-nav-mini") : b(".layui-layout-admin").addClass("admin-nav-mini"), e.removeNavHover())
            },
            // 设置导航栏选中
            activeNav: function(a) {
                a || (a = window.location.pathname);
                b(".layui-layout-admin .layui-side .layui-nav .layui-nav-item .layui-nav-child dd").removeClass("layui-this");
                b(".layui-layout-admin .layui-side .layui-nav .layui-nav-item").removeClass("layui-this");
                a && "" != a && (b(".layui-layout-admin .layui-side .layui-nav .layui-nav-item").removeClass("layui-nav-itemed"), a = b('.layui-layout-admin .layui-side .layui-nav a[lay-href="' + a + '"]'), a.parent().addClass("layui-this"), a.parent("dd").parents(".layui-nav-child").parent().addClass("layui-nav-itemed"))
            },
            // 右侧弹出
            popupRight: function(a) {
                var c = a.end;
                void 0 == a.title && (a.title = !1, a.closeBtn = !1);
                void 0 == a.anim && (a.anim = 2);
                void 0 == a.fixed && (a.fixed = !0);
                a.isOutAnim = !1;
                a.offset = "r";
                a.shadeClose = !0;
                a.area = "336px";
                a.skin = "layui-anim admin-anim-rl layui-layer-adminRight";
                a.move = !1;
                a.end = function() {
                    f.closeAll("tips");
                    c ? c : ""
                };
                return e.open(a)
            },
            // 打开弹窗
            open: function(a) {
                a.area || (a.area = 2 == a.type ? ["360px", "300px"] : "360px");
                a.skin || (a.skin = "layui-layer-admin");
                a.offset || (a.offset = "100px");
                void 0 == a.fixed && (a.fixed = !1);
                a.resize = void 0 != a.resize ? a.resize : !1;
                a.shade = void 0 != a.shade ? a.shade : .1;
                return f.open(a)
            },
            // req(url, data, success, method)	封装的ajax请求，req只返回json
            req: function(a, c, b, g) {
                "put" == g.toLowerCase() ? (g = "POST", c._method = "PUT") : "delete" == g.toLowerCase() && (g = "POST", c._method = "DELETE");
                e.ajax({
                    url: config.base_server + a,
                    data: c,
                    type: g,
                    dataType: "json",
                    success: b
                })
            },
            // admin.ajax不会再url前面添加config.base_server
            ajax: function(a) {
                var c = a.success;
                a.success = function(b, g, f) { //  b代表返回的数据
                    var d;
                    (d = "json" == a.dataType.toLowerCase() ? b : e.parseJSON(b)) && 0 == config.ajaxSuccessBefore(d) || c(b, g, f)
                };
                a.error = function(b) {
                    a.success({
                        code: b.status, //此处需根据后台字段
                        msg: b.statusText //此处需根据后台字段
                    })
                };
                a.beforeSend = function(a) { // 设置请求头
                    for (var b = config.getAjaxHeaders(), c = 0; c < b.length; c++) a.setRequestHeader(b[c].name, b[c].value)
                };
                b.ajax(a)
            },
            putTempData: function(a, b) {
                b ? layui.sessionData("tempData", {
                    key: a,
                    value: b
                }) : layui.sessionData("tempData", {
                    key: a,
                    remove: !0
                })
            },
            getTempData: function(a) {
                return layui.sessionData("tempData")[a]
            },
            parseJSON: function(a) {
                if ("string" == typeof a) try {
                    var b = JSON.parse(a);
                    if ("object" == typeof b && b) return b
                } catch (d) {}
            },
            rollPage: function(a) {
                var c = b(".layui-layout-admin .layui-body .layui-tab .layui-tab-title"),
                    d = c.scrollLeft();
                if ("left" === a) c.scrollLeft(d - 120);
                else if ("auto" === a) {
                    var e = 0;
                    c.children("li").each(function() {
                        if (b(this).hasClass("layui-this")) return !1;
                        e += b(this).outerWidth()
                    });
                    c.scrollLeft(e - 47)
                } else c.scrollLeft(d + 120)
            },
            refresh: function() {
                var a = b(".layui-layout-admin .layui-body .layui-tab-content .layui-tab-item.layui-show .admin-iframe");
                a || (a = b(".layui-layout-admin .layui-body>.admin-iframe"));
                a && a[0].contentWindow.location.reload(!0)
            },
            closeTabOperNav: function() {
                b(".layui-icon-down .layui-nav .layui-nav-child").removeClass("layui-show")
            },
            changeTheme: function(a) {
                top.removeTheme();
                !a || top.layui.link(a);
                for (var b = top.window.frames,
                        d = 0; d < b.length; d++) {
                    var e = b[d];
                    try {
                        e.removeTheme()
                    } catch (l) {}
                    a && e.layui && e.layui.link(a)
                }
            },
            closeThisDialog: function() {
                parent.layer.close(parent.layer.getFrameIndex(window.name))
            },
            iframeAuto: function() {
                parent.layer.iframeAuto(parent.layer.getFrameIndex(window.name))
            },
            getPageHeight: function() {
                return document.documentElement.clientHeight || document.body.clientHeight
            },
            getPageWidth: function() {
                return document.documentElement.clientWidth || document.body.clientWidth
            },
            removeNavHover: function() {
                b(".admin-nav-hover>.layui-nav-child").css({
                    top: "auto",
                    "max-height": "none",
                    overflow: "auto"
                });
                b(".admin-nav-hover").removeClass("admin-nav-hover")
            },
            setNavHoverCss: function(a) {
                var c = b(".admin-nav-hover>.layui-nav-child");
                if (a && 0 < c.length) {
                    if (a.offset().top + c.outerHeight() > window.innerHeight) {
                        var d = a.offset().top - c.outerHeight() + a.outerHeight();
                        50 > d ? (d = e.getPageHeight(), a.offset().top < d / 2 ? c.css({
                            top: "50px",
                            "max-height": d - 50 + "px",
                            overflow: "auto"
                        }) : c.css({
                            top: a.offset().top,
                            "max-height": d - a.offset().top,
                            overflow: "auto"
                        })) : c.css("top", d)
                    } else c.css("top", a.offset().top);
                    g = !0
                }
            },
            // 判断是否有权限 根据情况修改内部判断逻辑
            hasPerm: function(auth) {
                var user = config.getUser();
                if (user.authorities) {
                    for (var i = 0; i < user.authorities.length; i++) {
                        if (auth == user.authorities[i].authority) {
                            return true;
                        }
                    }
                }
                return false;
            },
            // 显示加载动画 
            showLoading: function(ele) {
                b(ele).append('<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop admin-loading"></i>');
            },
            // 移除加载动画
            removeLoading: function(ele) {
                b(ele + '>.admin-loading').remove();
            },
            getUrlParam: function() {
                var param = window.location.search;
                var pattern = /([^?&=]+)=([^&#]*)/g;
                var dict = {};
                var search = null;
                if (typeof param === "object" && param instanceof Location) {
                    search = param.search;
                } else if (typeof param === "string") {
                    search = param;
                } else {
                    throw new Error("参数类型非法！请传入window.loaction对象或者url字符串。");
                }
                search.replace(pattern, function(rs, $1, $2) {
                    var key = decodeURIComponent($1);
                    var value = decodeURIComponent($2);
                    dict[key] = value;
                    return rs;
                });
                return dict;
            },
            events: {
                flexible: function(a) {
                    a = b(".layui-layout-admin").hasClass("admin-nav-mini");
                    e.flexible(a)
                },
                back: function() {
                    history.back()
                },
                // theme: function () {
                //     var a = b(this).attr("data-url");
                //     e.popupRight({
                //         type: 2,
                //         content: a ? a : "page/tpl/tpl-theme.html"
                //     })
                // },
                fullScreen: function(a) {
                    a = b(this).find("i");
                    if (document.fullscreenElement || document.msFullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement) {
                        var c = document.exitFullscreen || document.webkitExitFullscreen || document.mozCancelFullScreen || document.msExitFullscreen;
                        c ? c.call(document) : window.ActiveXObject && (c = new ActiveXObject("WScript.Shell"), c.SendKeys("{F11}"));
                        a.addClass("layui-icon-screen-full").removeClass("layui-icon-screen-restore")
                    } else {
                        c = document.documentElement;
                        var d = c.requestFullscreen || c.webkitRequestFullscreen || c.mozRequestFullScreen || c.msRequestFullscreen;
                        d ? d.call(c) : window.ActiveXObject && (c = new ActiveXObject("WScript.Shell"), c.SendKeys("{F11}"));
                        a.addClass("layui-icon-screen-restore").removeClass("layui-icon-screen-full")
                    }
                },
                refresh: function() {
                    e.refresh()
                },
                leftPage: function() {
                    e.rollPage("left")
                },
                rightPage: function() {
                    e.rollPage()
                },
                closeThisTabs: function() {
                    e.closeTabOperNav();
                    var a = b(".layui-layout-admin .layui-body .layui-tab .layui-tab-title");
                    a.find("li").first().hasClass("layui-this") ? f.msg("\u4e3b\u9875\u4e0d\u80fd\u5173\u95ed", {
                        icon: 2
                    }) : a.find("li.layui-this").find(".layui-tab-close").trigger("click")
                },
                closeOtherTabs: function() {
                    b(".layui-layout-admin .layui-body .layui-tab .layui-tab-title li:gt(0):not(.layui-this)").find(".layui-tab-close").trigger("click");
                    e.closeTabOperNav()
                },
                closeAllTabs: function() {
                    b(".layui-layout-admin .layui-body .layui-tab .layui-tab-title li:gt(0)").find(".layui-tab-close").trigger("click");
                    b(".layui-layout-admin .layui-body .layui-tab .layui-tab-title li:eq(0)").trigger("click");
                    e.closeTabOperNav()
                },
                closeDialog: function() {
                    e.closeThisDialog()
                }
            }
        };
    b("body").on("click", "*[sb-event]",
        function() {
            var a = b(this).attr("sb-event");
            (a = e.events[a]) && a.call(this, b(this))
        });
    b(".site-mobile-shade").click(function() {
        e.flexible(!0)
    });
    var g = !1;
    b("body").on("mouseenter", ".layui-layout-admin.admin-nav-mini .layui-side .layui-nav .layui-nav-item>a",
        function() {
            if (750 < document.body.clientWidth) {
                var a = b(this);
                b(".admin-nav-hover>.layui-nav-child").css("top", "auto");
                b(".admin-nav-hover").removeClass("admin-nav-hover");
                a.parent().addClass("admin-nav-hover");
                if (0 < b(".admin-nav-hover>.layui-nav-child").length) e.setNavHoverCss(a);
                else {
                    var c = a.find("cite").text(),
                        d = b(".layui-layout-admin .layui-side").css("background-color");
                    f.tips(c, a, {
                        tips: [2, "rgb(255, 255, 255)" == d ? "#009688" : d],
                        time: -1
                    })
                }
            }
        }).on("mouseleave", ".layui-layout-admin.admin-nav-mini .layui-side .layui-nav .layui-nav-item>a",
        function() {
            f.closeAll("tips")
        });
    b("body").on("mouseleave", ".layui-layout-admin.admin-nav-mini .layui-side",
        function() {
            g = !1;
            setTimeout(function() {
                    g || e.removeNavHover()
                },
                500)
        });
    b("body").on("mouseenter", ".layui-layout-admin.admin-nav-mini .layui-side .layui-nav .layui-nav-item.admin-nav-hover .layui-nav-child",
        function() {
            g = !0
        });
    b("body").on("mouseenter", "*[lay-tips]",
        function() {
            var a = b(this).attr("lay-tips"),
                c = b(this).attr("lay-direction"),
                d = b(this).attr("lay-bg");
            f.tips(a, this, {
                tips: [c || 3, d || "#333333"],
                time: -1
            })
        }).on("mouseleave", "*[lay-tips]",
        function() {
            f.closeAll("tips")
        });
    b("body").on("click", "*[sb-href]",
        function() {
            var a = b(this).attr("sb-href"),
                c = b(this).text();
            top.layui.index.openTab({
                title: c,
                url: a
            })
        });
    exports("admin", e)
});